<?php
/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class ScoreController extends CommonController
{
    protected $user_id;
    protected $action;
    protected $back_act = '';
    /**
     * 构造函数
     */
    public function __construct()
    {
        parent::__construct();
        // 属性赋值
        $this->user_id = $_SESSION['user_id'];
        $this->action = ACTION_NAME;
        // 验证登录
        $this->check_login();
        // 用户信息
        $info = model('ClipsBase')->get_user_default($this->user_id);
        // 如果是显示页面，对页面进行相应赋值
        assign_template();
        $this->assign('action', $this->action);
        $this->assign('info', $info);
    }

    public function index() {
    		$where = "user_id= ".$this->user_id." and  fs_type in(1,2,3) and FROM_UNIXTIME(change_time,'%Y-%m') ='".date('Y-m')."'";
		$sql = 'SELECT log_id,fs_type,change_desc,pay_points,change_time FROM ' . $this->model->pre . 'account_log where '.$where." order by log_id desc";
		$rs = $this->model->query($sql);
		$array = array();
		foreach ($rs as $key => $value) {
			$get_times = date('Y-m-d',$value['change_time']);
			if(!$array[$get_times]){
				$array[$get_times] = array( 'sign'=>0, 'turn'=>0, 'share'=>0,'fsdate'=> $get_times);
			}
			switch ($value['fs_type']) {
				case '1'://文章
					$array[$get_times]['share'] = 1;
					break;
				case '2'://抽奖
					$array[$get_times]['turn'] = 1;
					break;
				case '3'://签到
					$array[$get_times]['sign'] = 1;
					break;
				default:
					break;
			}
		}
		$this->assign('record', $array);
        $this->display('SignIn-page.dwt');
    }

    public function ajax_add_score() {
		$t = I('get.t') ? I('get.t') : 0;
		if($t){
			$qian_num = $this->model->table('account_log')->field('count(*)')->where( "user_id= ".$this->user_id." and fs_type = 3 and is_oldadd = 1 and FROM_UNIXTIME(change_time,'%Y-%m') ='".date("Y-m",strtotime("-1 day"))."'" )->getOne();

			$where = "user_id= ".$this->user_id." and fs_type = 3 and FROM_UNIXTIME(change_time,'%Y-%m-%d') ='".date("Y-m-d",strtotime("-1 day"))."'";
		}else{
			$where = "user_id= ".$this->user_id." and fs_type = 3 and FROM_UNIXTIME(change_time,'%Y-%m-%d') ='".date('Y-m-d')."'";
		}
		$sql = 'SELECT count(log_id) as sign_number FROM ' . $this->model->pre . 'account_log where '.$where;
        $rs = $this->model->query($sql);
        if($qian_num >= 2){
			echo 2;
		}elseif($rs[0]['sign_number'] >= 1 || empty($this->user_id)){
			echo 0;
        }else{
			if($t){
				$sql = "INSERT INTO " . $this->model->pre . "account_log (`log_id`, `user_id`, `user_money`, `frozen_money`, `rank_points`, `pay_points`, `change_time`, `change_desc`,`fs_type`, `is_oldadd`) VALUES";
				$sql .= "(NULL, '{$this->user_id}', '0.00', '0.00', '0', '1', '".strtotime("-1 day")."', '签到获得积分','3', '1');";
				$rs = $this->model->query($sql);
				if($rs){
					$udpate_sql = "update " . $this->model->pre . "users set pay_points = pay_points + 1 where user_id = '".$this->user_id."'";
					$this->model->query($udpate_sql);
				}
			}else{
				 model('ClipsBase')->log_account_change($this->user_id, 0, 0, 0, 1, "签到获得积分", ACT_OTHER, 3);
			}

			$time_num = model('Integral')->check_time();
			if($time_num == 7){
				model('ClipsBase')->log_account_change($this->user_id, 0, 0, 0, 10, "连续签到7天获得积分", ACT_OTHER, 3);
			}elseif($time_num == 15){
				model('ClipsBase')->log_account_change($this->user_id, 0, 0, 0, 20, "连续签到15天获得积分", ACT_OTHER, 3);
			}elseif($time_num == 30){
				model('ClipsBase')->log_account_change($this->user_id, 0, 0, 0, 40, "连续签到30天获得积分", ACT_OTHER, 3);
			}
			echo 1;
        }
    }


     /**  未登录验证 */
    private function check_login()
    {
        // 不需要登录的操作或自己验证是否登录（如ajax处理）的方法
        $without = array(
            'login',
            'register',
            'get_password_phone',
            'get_password_email',
            'get_password_question',
            'pwd_question_name',
            'send_pwd_email',
            'edit_password',
            'check_answer',
            'logout',
            'clear_histroy',
            'add_collection',
            'third_login'
        );
        // 未登录处理
        if (empty($_SESSION['user_id']) && ! in_array($this->action, $without)) {
            $url = __HOST__ . $_SERVER['REQUEST_URI'];
            $this->redirect(url('login', array(
                'referer' => urlencode($url)
            )));
            exit();
        }
        
        // 已经登录，不能访问的方法
        $deny = array(
            'login',
            'register'
        );
        if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0 && in_array($this->action, $deny)) {
            $this->redirect(url('index/index'));
            exit();
        }
    }

}
